/*
 * Copyright (c) 2010-2011 Plugger Antonio Begue Ponce. All Rights Reserved.
 *
 * This file is part of Plugger Framework.
 *
 *  Plugger Framework is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License version 3 as published by
 *  the Free Software Foundation.
 *
 *  Plugger Framework is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Plugger Framework.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.plugger.repository.impl.hibernate;

import java.io.File;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.plugger.data.DataConfiguration;

/**
 * A singleton static class that get a hibernate session factory.
 *
 * @author Antonio Begue
 * @version $Revision: 1.0 $
 */
public class HConfig {
    private static final SessionFactory sessionFactory;

    static {
        Configuration config = new Configuration();
        configure(config);
        sessionFactory = config.buildSessionFactory();
    }

    /**
     * Return the singleton SessionFactory object
     * @return A SessionFactory object
     */
    public static SessionFactory getSessionfactory() {
        return sessionFactory;
    }

    /**
     * Configure the O/R hibernate service.
     * @param config A hibernate Configuration object.
     */
    public static void configure(Configuration config) {
        config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

        config.setProperty("hibernate.show_sql", "true");

        config.setProperty("dialect", DataConfiguration.getMapperParameters().getDialect());
        config.setProperty("connection.driver_class", DataConfiguration.getMapperParameters().getDriverClass());
        config.setProperty("connection.url", DataConfiguration.getConnectionString().toString());
        config.setProperty("connection.isolation", DataConfiguration.getMapperParameters().getIsolation());
        config.setProperty("connection.username", DataConfiguration.getConnectionString().getUser());
        config.setProperty("connection.password", DataConfiguration.getConnectionString().getPassword());

        config.setProperty("hibernate.dialect", DataConfiguration.getMapperParameters().getDialect());
        config.setProperty("hibernate.connection.driver_class", DataConfiguration.getMapperParameters().getDriverClass());
        config.setProperty("hibernate.connection.url", DataConfiguration.getConnectionString().toString());
        config.setProperty("hibernate.connection.isolation", DataConfiguration.getMapperParameters().getIsolation());

//        config.setProperty("hibernate.c3p0.min_size", "5");
//        config.setProperty("hibernate.c3p0.max_size", "20");
//        config.setProperty("hibernate.c3p0.timeout", "1800");
//        config.setProperty("hibernate.c3p0.max_statements", "50");
        config.setProperty("hibernate.connection.pool_size", "0");

        config.setProperty("hibernate.connection.username", DataConfiguration.getConnectionString().getUser());
        config.setProperty("hibernate.connection.password", DataConfiguration.getConnectionString().getPassword());

        String path = ".";
        if (new File(path + "\\dist").exists()) {
            path += "\\dist";
        }
        if (new File(path + "\\lib").exists()) {
            path += "\\lib";
        }
        path += "\\";
        
        File file = null;
        for (String assembly : DataConfiguration.getAssemblies()) {
            file = new File(path + assembly);
            config.addJar(file);
        }
    }
}



//config.setProperty("dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
//
//config.setProperty("dialect", "org.hibernate.dialect.MySQL5Dialect");
//config.setProperty("connection.driver_class", "org.gjt.mm.mysql.Driver");
//config.setProperty("connection.url", "jdbc:mysql://192.168.1.202/pluggerdb");
//config.setProperty("connection.username", "abegue");
//config.setProperty("connection.password", "asterix");
//
//config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
//config.setProperty("hibernate.connection.driver_class", "org.gjt.mm.mysql.Driver");
//config.setProperty("hibernate.connection.url", "jdbc:mysql://192.168.1.202/pluggerdb");
//config.setProperty("hibernate.connection.username", "abegue");
//config.setProperty("hibernate.connection.password", "asterix");
//
//File file = new File("C:\\Users\\abegue\\Documents\\NetBeansProjects\\plugger.test\\dist\\plugger.test.jar");
//File file = new File(".\\dist\\lib\\plugger.cms.jar");
//config.addJar(file);



//<property name="pool_size">5</property>
//<property name="show_sql">false</property>
//<!-- Database connection settings -->
//<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
//<property name="connection.url">jdbc:mysql://localhost/register</property>
//<property name="connection.username">test</property>
//<property name="connection.password">test</property>
//
//<!-- SQL dialect -->
//<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
//
//<!-- Echo all executed SQL to stdout -->
//<property name="show_sql">true</property>
//
//<property name="current_session_context_class">thread</property>
//
//<mapping resource="com/icesoft/icefaces/tutorial/crud/hibernate/Course.hbm.xml"/>
//<mapping resource="com/icesoft/icefaces/tutorial/crud/hibernate/Student.hbm.xml"/>
//Configuration cfg = new Configuration()
//.addClass(org.hibernate.auction.Item.class)
//.addClass(org.hibernate.auction.Bid.class)
//.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
//.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
//.setProperty("hibernate.order_updates", "true");
//hibernate.connection.driver_class	JDBC driver class
//hibernate.connection.url	JDBC URL
//hibernate.connection.username	database user
//hibernate.connection.password	database user password
//hibernate.connection.pool_size	maximum number of pooled connections
//hibernate.connection.driver_class = org.postgresql.Driver
//hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
//hibernate.connection.username = myuser
//hibernate.connection.password = secret
//hibernate.c3p0.min_size=5
//hibernate.c3p0.max_size=20
//hibernate.c3p0.timeout=1800
//hibernate.c3p0.max_statements=50
//hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect




